/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'question_answer',
                condition: {},
                model: {
                    url:_global.url.api + 'question_answer',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                    },
                    edit: function(e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        dialog({
                            'title':'修改页面',
                            'content': template('add-tpl',this.data),
                            'width':600,
                            ok: function() {
                                _this.data.question_type = $("select[name='question_type']").val();
                                _this.data.question = $("input[name='question']").val();
                                if(_this.data.question==''||_this.data.question==undefined){
                                    Alert({msg:'请填写问题描述',type:'error'});
                                    return false;
                                }
                                _this.data.sort = $("input[name='sort']").val();
                                if(_this.data.sort==''||_this.data.sort==undefined){
                                    Alert({msg:'请填写排序',type:'error'});
                                    return false;
                                }
                                _this.data.answer_link = $("input[name='answer_link']").val();
                                if(_this.data.answer_link==''||_this.data.answer_link==undefined){
                                    Alert({msg:'请填写答案链接',type:'error'});
                                    return false;
                                }
                                if(model.put()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                        var s_value=$("select[name='question_type']").attr("rel");
                        $("select[name='question_type']").prop("value",s_value);
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;

            /**
             * 按问题类型查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.question_type = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });

            //保存
            $('.btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title:'添加问题',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function () {
                        _this.data.question_type = $("select[name='question_type']").val();
                        _this.data.question = $("input[name='question']").val();
                        if(_this.data.question==''||_this.data.question==undefined){
                            Alert({msg:'请填写问题描述',type:'error'});
                            return false;
                        }
                        _this.data.sort = $("input[name='sort']").val();
                        if(_this.data.sort==''||_this.data.sort==undefined){
                            Alert({msg:'请填写排序',type:'error'});
                            return false;
                        }
                        _this.data.answer_link = $("input[name='answer_link']").val();
                        if(_this.data.answer_link==''||_this.data.answer_link==undefined){
                            Alert({msg:'请填写答案链接',type:'error'});
                            return false;
                        }
                        if(new Model({
                                url: _global.url.api + 'question_answer',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    },cancel: function () {}
                });
                _this.dialog.show();
            });
        },
    };

    new index();

});